#include "game_menu.hpp"

#include "../gralaby/gralaby.hpp"
#include "../gunbuns/gunbuns.hpp"
#include "../snake/snake.hpp"
#include "../miniraces/miniraces.hpp"
#include "../shamrocket/shamrocket.hpp"
#include "../minesweeper/minesweeper.hpp"
#include "../pongfrenzy/pongfrenzy.hpp"

#include <iostream>

GameMenu::GameMenu() {

}

GameMenu::~GameMenu() {

}

void GameMenu::init(Engine *engine) {

    selectedMenuItem = 0;

	sf::String gralaby;
	gralaby.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	gralaby.SetText("Gralaby");
	gralaby.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 260));
	gralaby.SetCenter(sf::Vector2f(gralaby.GetRect().GetWidth() / 2.f, gralaby.GetRect().GetHeight() / 2.f));
	gralaby.SetColor(sf::Color(255, 255, 255, 255));
	gralaby.SetScale(sf::Vector2f(1.2f, 1.2f));

	sf::String gunBuns;
	gunBuns.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	gunBuns.SetText("GunBuns");
	gunBuns.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 300));
	gunBuns.SetCenter(sf::Vector2f(gunBuns.GetRect().GetWidth() / 2.f, gunBuns.GetRect().GetHeight() / 2.f));
	gunBuns.SetColor(sf::Color(255, 255, 255, 255));

	sf::String snake;
	snake.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	snake.SetText("Snake");
	snake.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 340));
	snake.SetCenter(sf::Vector2f(snake.GetRect().GetWidth() / 2.f, snake.GetRect().GetHeight() / 2.f));
	snake.SetColor(sf::Color(255, 255, 255, 255));

	sf::String miniRaces;
	miniRaces.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	miniRaces.SetText("MiniRaces");
	miniRaces.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 380));
	miniRaces.SetCenter(sf::Vector2f(miniRaces.GetRect().GetWidth() / 2.f, miniRaces.GetRect().GetHeight() / 2.f));
	miniRaces.SetColor(sf::Color(255, 255, 255, 255));

	sf::String shamRocket;
	shamRocket.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	shamRocket.SetText("ShamRocket");
	shamRocket.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 420));
	shamRocket.SetCenter(sf::Vector2f(shamRocket.GetRect().GetWidth() / 2.f, shamRocket.GetRect().GetHeight() / 2.f));
	shamRocket.SetColor(sf::Color(255, 255, 255, 255));

	sf::String mineSweeper;
	mineSweeper.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	mineSweeper.SetText("MineSweeper");
	mineSweeper.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 460));
	mineSweeper.SetCenter(sf::Vector2f(mineSweeper.GetRect().GetWidth() / 2.f, mineSweeper.GetRect().GetHeight() / 2.f));
	mineSweeper.SetColor(sf::Color(255, 255, 255, 255));

	sf::String pongFrenzy;
	pongFrenzy.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	pongFrenzy.SetText("PongFrenzy");
	pongFrenzy.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 500));
	pongFrenzy.SetCenter(sf::Vector2f(pongFrenzy.GetRect().GetWidth() / 2.f, pongFrenzy.GetRect().GetHeight() / 2.f));
	pongFrenzy.SetColor(sf::Color(255, 255, 255, 255));

    sf::String back;
	back.SetFont(engine->getFont("data/fonts/BELIEVER.TTF"));
	back.SetText("Back");
	back.SetPosition(sf::Vector2f(engine->window.GetWidth() / 2.f, 560));
	back.SetCenter(sf::Vector2f(back.GetRect().GetWidth() / 2.f, back.GetRect().GetHeight() / 2.f));
	back.SetColor(sf::Color(255, 255, 255, 255));

	menuItems.push_back(gralaby);
	menuItems.push_back(gunBuns);
	menuItems.push_back(snake);
	menuItems.push_back(miniRaces);
	menuItems.push_back(shamRocket);
	menuItems.push_back(mineSweeper);
	menuItems.push_back(pongFrenzy);
	menuItems.push_back(back);

	background.SetImage(engine->getImage("data/images/main-menu.png"));

	/*themeMusic.Open("data/music/Goof.ogg"); //STFU
	themeMusic.Play();*/
}

void GameMenu::cleanup() {
	//themeMusic.Stop();
	menuItems.clear();
}

void GameMenu::pause() {
	//themeMusic.Stop();
}

void GameMenu::resume() {
    //themeMusic.Play();
}

void GameMenu::processEvent(Engine *engine, PlayerEvent& event) {
	switch(event.type) {
		case PlayerEvent::MoveUp:
		{
			if(event.state)
				if(selectedMenuItem > 0) {
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.f, 1.f));
					selectedMenuItem--;
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.2f, 1.2f));
				}
			break;
		}
		case PlayerEvent::MoveDown:
		{
			if(event.state)
				if(selectedMenuItem < menuItems.size() - 1) {
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.f, 1.f));
					selectedMenuItem++;
					menuItems[selectedMenuItem].SetScale(sf::Vector2f(1.2f, 1.2f));
				}
			break;
		}
		case PlayerEvent::Action1:
		{
			if(event.state) {
				switch(selectedMenuItem) {
					case 0:
						//if(gralaby.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY()))
						engine->pushState(Gralaby::getSingleton());
						break;
					case 1:
						//if(gunBuns.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY()))
						engine->pushState(GunBuns::getSingleton());
						break;
					case 2:
						engine->pushState(Snake::getSingleton());
						break;
					case 3:
						engine->pushState(MiniRaces::getSingleton());
						break;
					case 4:
						engine->pushState(ShamRocket::getSingleton());
						break;
					case 5:
						engine->pushState(MineSweeper::getSingleton());
						break;
					case 6:
						engine->pushState(PongFrenzy::getSingleton());
						break;
					default:
						//else if(quit.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY()))
						engine->popState();
						break;
				}
			}
			break;
		}
		case PlayerEvent::Quit:
		{
				if(event.state)
					engine->popState();
			break;
		}
		default:
			break;
	}
}

void GameMenu::update(Engine *engine) {
	/*if(gralaby.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY())) gralaby.SetScale(sf::Vector2f(1.2f, 1.2f));
	else gralaby.SetScale(sf::Vector2f(1.0f, 1.0f));

	if(gunBuns.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY())) gunBuns.SetScale(sf::Vector2f(1.2f, 1.2f));
	else gunBuns.SetScale(sf::Vector2f(1.0f, 1.0f));

	if(quit.GetRect().Contains(engine->window.GetInput().GetMouseX(), engine->window.GetInput().GetMouseY())) quit.SetScale(sf::Vector2f(1.2f, 1.2f));
	else quit.SetScale(sf::Vector2f(1.0f, 1.0f));*/
}

void GameMenu::draw(Engine *engine) {
	engine->window.Draw(background);

	for(std::vector<sf::String>::iterator iMenuItem = menuItems.begin(); iMenuItem != menuItems.end(); iMenuItem++)
		engine->window.Draw(*iMenuItem);
}
